package com.skeyedu.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5utils {
	// 设置的默认盐值,用于盐值混合加密
	private static final String slat = "&%5123****&&%%$$#@";
	
	// 生成值是32位长度的数字和字符串的混合
	public static String encrypt(String dataStr) {
		try {
			dataStr = dataStr + slat;
			MessageDigest md = MessageDigest.getInstance("MD5");
		
			md.update(dataStr.getBytes("UTF8"));
			byte s[] = md.digest();
			String result = "";
			for (int i = 0; i < s.length; i++) {
				result += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
			}
			return result.toUpperCase();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	// 生成值是38位长度的纯数字
	public static String encryptInteger (String plainText) {
		byte[] secretBytes = null;
		try {
			secretBytes = MessageDigest.getInstance("md5")
					.digest(plainText.getBytes());
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("没有这个MD5算法!");
		}
		
		String code = new BigInteger(1, secretBytes).toString();
		for (int i = 0; i < 24 - code.length(); i++) {
			code += code;
		}
		
		return code.toUpperCase();
	}
	
	public static void main(String[] args) {
		// 自定义工具类 MD5+盐 数字和字母混合
		System.out.println(MD5utils.encrypt("admin"));
		System.out.println(MD5utils.encrypt("admin").length());
		// 自定义工具类 MD5+盐 纯数字
		System.out.println(MD5utils.encryptInteger("admin"));
		System.out.println(MD5utils.encryptInteger("admin").length());
		// hutool MD5+盐
		System.out.println(SecureUtil.md5("admin"+slat).toUpperCase());
		System.out.println(SecureUtil.md5("admin").length());
		// UUID生成
		System.out.println(IdUtil.simpleUUID().toUpperCase());
	}
}
